var deptId = ""
var echo = ""
var userId = ""

layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 通知中心管理
     */
    var DbNotice = {
        tableId: "dbNoticeTable"
    };


    var ajax = new $ax(Feng.ctxPath + '/databaseListKh/getUser')
    var result = ajax.start();
    // console.log(result)
    deptId = result.data.deptId
    userId = result.data.id
    if (deptId == 25) {
        echo = true
    } else {
        echo = false
    }

    /**
     * 初始化表格的列
     */
    DbNotice.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'id', style: 'background-color:#ffffff'},
            {field: 'rank', title: '序号', align: 'center', templet: '#rank', style: 'background-color:#ffffff'},
            {field: 'content', align: 'center', title: '消息内容', style: 'background-color:#ffffff'},
            {field: 'department', align: 'center', title: '单位', style: 'background-color:#ffffff', hide: true},
            {field: 'system', align: 'center', title: '应用系统', style: 'background-color:#ffffff'},
            {
                field: 'state', align: 'center', title: '状态', templet: function (d) {
                    if (d.state == 0) {
                        return "<span style='color:red'>未读</span>"
                    } else if (d.state == 1) {
                        return "已读"
                    }
                }, style: 'background-color:#ffffff', hide: echo
            },
            {
                field: 'adminState', align: 'center', title: '状态', templet: function (d) {
                    if (d.adminState == 0) {
                        return "<span style='color:red'>未读</span>"
                    } else if (d.adminState == 1) {
                        return "已读"
                    }
                }, style: 'background-color:#ffffff', hide: !echo
            },
            {align: 'center', toolbar: '#tableBar', title: '', style: 'background-color:#ffffff'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DbNotice.search = function () {
        var queryData = {};


        table.reload(DbNotice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击查看详情
     *
     * @param data 点击按钮时候的行数据
     */
    DbNotice.openEditDlg = function (data) {
        func.open({
            title: '',
            content: Feng.ctxPath + '/dbNotice/edit?id=' + data.id,
            tableId: DbNotice.tableId,
            end: table.reload(DbNotice.tableId),
            cancel: function (index, func) {
                table.reload(DbNotice.tableId)
            }
        });
        if (deptId == 25) {
            $.ajax(Feng.ctxPath + '/dbNotice/editItem', {
                data: {
                    adminState: 1,
                    id: data.id
                },
                success: function () {
                    table.reload(DbNotice.tableId)
                }
            })
        } else {
            $.ajax(Feng.ctxPath + '/dbNotice/editItem', {
                data: {
                    state: 1,
                    id: data.id
                },
                success: function () {
                    table.reload(DbNotice.tableId)
                }
            })
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DbNotice.tableId,
        url: Feng.ctxPath + '/dbNotice/list?userId=' + userId,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: 'line',
        cols: DbNotice.initColumn(),
        done: function () {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 工具条点击事件
    table.on('tool(' + DbNotice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DbNotice.openEditDlg(data);
        }
    });
});
